package com.gjintel.framework.role.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.menu.model.RoleModel;
import com.gjintel.framework.role.dao.IRoleDao;
import com.gjintel.framework.role.model.ProjectOperation;
import com.gjintel.framework.role.model.Role;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.user.model.UserRole;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 角色数据访问实现
 * @author fengya
 * @date 2015-7-23 下午04:28:05
 */
@Repository("roleDao")
@SuppressWarnings("all")
public class RoleDaoImpl extends HibernateGenericDAO implements IRoleDao {

	@Override
	public void findAll(Page page, Role role, Sort sort) {
		StringBuilder hql = new StringBuilder("from Role r where r.flag=0 and r.id!='1'");
		List<String> params = new ArrayList<String>(1);
		if (role != null) {
			if (!StringUtil.isEmpty(role.getName())) {
				hql.append(" and r.name like ? ");
				params.add("%" + role.getName().trim() + "%");
			}
		}
		this.findAsPage(hql.toString(), page, sort, "r", true, params.toArray());
	}

	@SuppressWarnings("unchecked")
	public List<Role> findAll() {
		return this.find("from Role r");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleModel> getRoleModelByRoleId(String roleId) {
		return this.find("from RoleModel rm where rm.roleId=?", roleId);
	}

	@Override
	public UserRole getUserRole(String roleId, String userId) {
		Object obj = this.findUnique("from UserRole ur where ur.userId=? and ur.roleId=?", userId, roleId);
		if (obj != null) {
			return (UserRole) obj;
		} else {
			return null;
		}
	}

	@Override
	public List<Role> findRoleListExcludeAdmin(String bidderId) {
		return this.find("from Role r where r.id != 1 and r.id != ?",bidderId );
	}

	@Override
	public List<Role> findRolesByUserId(String userIdStr) {
		// SELECT * FROM `t_sys_user_role` ur INNER JOIN `t_sys_role_info` r ON ur.`ROLE_ID`=r.ID WHERE `USER_ID`='40288491569164e6015691dd9f570023';
		return this.find("select r from Role r,UserRole ur where ur.roleId=r.id and ur.userId='"+userIdStr+"'");
	}

	@Override
	public List<User> findUserByRoleId(String RoleId) {
		return this.find("select u from User u,UserRole ur where ur.userId=u.id and u.flag=0 and ur.roleId='"+RoleId+"'");
	}

	@Override
	public List<ProjectOperation> findOperationByRoleIdAndStatus(String roleIds,
			String status,String workflowKey) {
		//SELECT * FROM `t_proj_operation` op INNER JOIN `t_proj_op_role_asso` opr ON op.id=opr.operation_id WHERE op.proj_status='4' AND opr.role_id='40288491568d88d901568d8e2ccf0000';
		return this.find("select new ProjectOperation(op.id, op.operaName, op.actionUrl, op.comment, op.projectStatus, op.seqs, opr.roleId) from ProjectOperation op,ProjectOperationRole opr"
				+ " where op.id=opr.operationId and opr.roleId in("+roleIds+") and op.projectStatus="+status+" and op.workflowKey='"+workflowKey+"' order by op.seqs");
	}

	@Override
	public List<Role> getRoleListExcludeAdmin() {
		return this.find("from Role r where r.id != 1");
	}

}
